// pages/food/index.js
const App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img: App.Imges.food,
    xin: App.Imges.xin,
    xin1: App.Imges.xin1,
    limit: 20,//默认条数
    offset: 1,//初始页码
    doneLoading: false,
    dataState: true,
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.move();
  },
  //收藏
  collection(e){
    let id = e.currentTarget.dataset.articleid,list=this.data.list;
    App.ajax('mp/collection', 'POST', { article_id: id }).then(res => {
      list.map((i)=>{
        if (i.id == res.data.article.id){
          i.collection = res.data.collection
        }
      })
      this.setData({
        list
      })

    }).catch(err => {
      console.log('报错了', err)
    })
  },
  //取消收藏 
  upcollection(e){
    let id = e.currentTarget.dataset.articleid, list = this.data.list;
    App.ajax(`mp/collection/${id}`, 'DELETE').then(res => {
      list.map((i) => {
        if (i.id == id) {
          i.collection = res.data.collection
        }
      })
      this.setData({
        list
      })

    }).catch(err => {
      console.log('报错了', err)
    })
  },
  move() {
    let list = this.data.list, limit = this.data.limit, offset = this.data.offset;
    App.ajax('mp/article/list', 'GET', { limit: limit, offset: offset, article_column_id:'1' }).then(res => {
      if (res.data.length == 0) {
        this.setData({
          dataState: false
        })
      }
      if (res && res.data) {
        res.data.map((i) => {
          list.push(i)
        })
        this.setData({
          list,
          doneLoading: false
        })
      }

    }).catch(err => {
      console.log('报错了', err)
    })
  },
  update() {
    console.log(this.data.dataState, this.data.doneLoading)
    if (!this.data.dataState || this.data.doneLoading) { return }

    let offset = this.data.offset, limit = this.data.limit, that = this;
    offset += 1;
    this.setData({
      offset,
      doneLoading: true
    }, () => {
      this.move();
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      offset:1,
      doneLoading: false,
      dataState: true,
      list: []
    },()=>{
      this.move()
    })
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})




